define([
        'app/utils/WebUtils',
        'app/service/GeneralServices',
        'underscore',
        'app/utils/AMUIPaginationUtils',
        'app/utils/AMUICookieUtils',
        'app/utils/CommonUtils'
        
        
        ], function (
        WebUtils,
        gs,
        _,
        AMUIPaginationUtils,
        AMUICookieUtils
        
    ) {
	
	var opt ={};
	var p1 = {};
	
	opt = AMUICookieUtils.initializePage();
	p1 = AMUICookieUtils.initializePage();
	
	p1.s=$.getUrlParam("s");
	p1.p=$.getUrlParam("p");
	p1.w=$.unicodeToHanzi($.getUrlParam("w"));
	p1.c=$.getUrlParam("c"); //cataloguuid
	
	opt.s=$.getUrlParam("s");
	opt.p=$.getUrlParam("p");
	opt.w=$.unicodeToHanzi($.getUrlParam("w"));
	opt.c = $.getUrlParam("c"); //cataloguuid
	
	console.log(opt);
	$("#wjt_search_input").val(p1.w);
	
	
	function index_urlSearch (w,p,c,s){
		if($.isNullStr(c)){
			c = "";
		}
		if($.isNull(s)){
    		s = $.paginationDefaultSize;
    	}
    	location.search="?w="+$.toUnicode(w)+"&p="+p+"&s="+s+"&c="+c;
	}
	
	
	gs.wjt_admin_init(p1, function(resp){
		if(resp.status == "500"){
			WebUtils.alert("连接错误，请刷新页面重试！");	
			return;
		}
		
		opt.navi_main = "我的微路演";
		opt.navi_sub = "创建你的知识微路演"
		
		opt.copyrighttext = resp.result.admin_copyright;//"© 2016 版权所有 中研沃兮网络科技（上海）有限公司";
		WebUtils.inputInt($("#wjt-admin-copyright"), opt.copyrighttext);
		
		WebUtils.adminPageInt(opt);
		
		$(".wjt-admin-list").html("");
		
		for(var i = 0; i < resp.result.rooms.length; i++){
			var item = resp.result.rooms[i];
			var _pwd = "无";
			if(!$.isNullStr(item.pwd)){
				_pwd = item.pwd;
			}
			
			var didAllowDiscussing = "";
			if(item.type == "3"){
				didAllowDiscussing = '<span class="am-badge am-badge-danger">已禁言</span>';
			}else{
				didAllowDiscussing = '<span class="am-badge am-badge-success">不禁言</span>';
			}
			var el = $(
					'<tr>'+
		            '<td><span class="wjt-am-popover" data-am-popover="{content: \''+item.name+'\', trigger: \'hover focus\'}">'+$.shortForStr(item.name,20)+'</span></td>'+
		            '<td>'+$.timeFormat1(item.time)+'</td> '+
		            '<td><span class="am-badge am-badge-success">'+item.attendeecount+'人</span></td>'+
		            '<td>'+
		            	'<span class="am-badge am-badge-danger">'+
		            		_pwd+
						'</span>'+
					  '</td>'+
		            '<td>'+
		              '<div class="am-dropdown" data-am-dropdown="">'+
		                '<button class="am-btn am-btn-default am-btn-xs am-dropdown-toggle" data-am-dropdown-toggle=""><span class="am-icon-cog"></span> <span class="am-icon-caret-down"></span></button>'+
		                '<ul class="am-dropdown-content">'+
//						  '<li><a href="#">主持人入口</a></li>'+
		                  '<li><a href="admin-roomdetails.html?roomuuid='+item.uuid+'">编辑／完善信息</a></li>'+
		                  '<li><a href="admin-roomdetails.html?roomuuid='+item.uuid+'&tab=2">设置Logo与封面</a></li>'+
		                  '<li><a href="admin-roomdetails.html?roomuuid='+item.uuid+'&tab=3">添加幻灯片或视频源</a></li>'+
		                  '<li><a href="admin-roomdetails.html?roomuuid='+item.uuid+'&tab=5">设置访问密码</a></li>'+
//		                  '<li><a href="#">生成微信二维码</a></li>'+
		                  '<li><a href="admin-chairinvite.html?roomuuid='+item.uuid+'">设置主讲嘉宾</a></li>'+
//		                  '<li><a href="#">主持人管理</a></li>'+
//		                  '<li><a href="#">禁言设置：'+didAllowDiscussing+'</a></li>'+
		                  '<li><a href="javascript:void(0)" class="wjt-room-del" data-roomname="'+item.name+'" data-roomuuid="'+item.uuid+'">彻底删除</a></li>'+
		                '</ul>'+
		              '</div>'+
		            '</td>'+
		          '</tr>');
			$(".wjt-admin-list").append(el);
		}
		
		//微路演列表
		var catalogs = resp.result.catalogs;
		if(typeof(catalogs.length) !="undefined"){
			if(catalogs.length > 1){
				//$("#wjt_room_catalog").append("<option value=\"\">全部</option>");
				for(var i = 2; i < catalogs.length; i++){
					var option_item = "<option value=\""+catalogs[i].uuid+"\">"+catalogs[i].name+"</option>";
					$("#wjt_room_catalog").append(option_item);	
				}
			}
			
			$("#wjt_room_catalog").data("init","1");
			$("#wjt_room_catalog").val(opt.c);
			
		}else{
			$(".wjt_room_catalog_container").hide();
		}
		
		//下拉栏
		$('.am-dropdown').dropdown();
		//弹出框
		$('.wjt-am-popover').popover();
		
		//初始化pagination
		if(resp.result.total <= $.paginationDefaultSize){
			var page_total = 1;	
		}else{
			var page_total = parseInt((resp.result.total-1)/$.paginationDefaultSize) + 1; 
		}
		AMUIPaginationUtils.init($(".wjt-pagination-container"), page_total, resp.result.current, 
			function(page){
				index_urlSearch($("#wjt_search_input").val(),page,opt.c);
			}
		);
		

		//delete
		$(".wjt-room-del").click(function(){
			var selected_roomuuid = $(this).data("roomuuid");
			var selected_roomname = $(this).data("roomname");
			var p ={
				roomuuid:selected_roomuuid,
				roomname:selected_roomname
			};
			
			WebUtils.confirm('是否删除此微路演【'+selected_roomname+'】，删除后将不能被恢复<input id="wjt_room_del_roomuuid_input" type="hidden" value="'+selected_roomuuid+'">',function(param){
				var input = $("#wjt_room_del_roomuuid_input").val();
				console.log(input);
				gs.wjt_delete_room({roomuuid:input},function(resp){
					$.refreshPage();
				});
			},function(){});
			
		});
		
	});
	
	
	$(".wjt-search-btn").click(function(){
		
		index_urlSearch($("#wjt_search_input").val(),1,opt.c);
		
	});
	
	//
	$('#wjt_room_catalog').change(function(){
		
		
		var init = $("#wjt_room_catalog").data("init");
		
		if(init != "1"){
			var cataloguuid = $.val_trim($(this).val());
			opt.cataloguuid = cataloguuid;

			index_urlSearch($("#wjt_search_input").val(),"1", cataloguuid);
		}
		$("#wjt_room_catalog").data("init","0");
		

	});
	
	return;
});